﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using BLL;

namespace TaoBaoProject.Handler.RegisterHandler
{
  /// <summary>
  /// UpdateRegisterHandler 的摘要说明
  /// </summary>
  public class UpdateRegisterHandler : IHttpHandler
  {
    protected static readonly log4net.ILog log =
        log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public string q1, q2;
    QQRelation qqrelationModel;
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      int id = Convert.ToInt32(context.Request.Params["id"]);
      string oldQQRelation = context.Request.Params["OldQQRelation"];
      string qq = context.Request.Params["QQ"] == null ? "" : context.Request.Params["QQ"].ToString().Trim();
      string wangwang = context.Request.Params["Wangwang"] == null ? "" : context.Request.Params["Wangwang"].ToString().Trim();
      string zhifubaoaccount = context.Request.Params["ZhifubaoAccount"] == null ? "" : context.Request.Params["ZhifubaoAccount"].ToString().Trim();
      string zhifubaoname = context.Request.Params["ZhifubaoName"] == null ? "" : context.Request.Params["ZhifubaoName"].ToString().Trim();
      string rebatezhifubao = context.Request.Params["RebateZhifubao"] == null ? "" : context.Request.Params["RebateZhifubao"].ToString().Trim();
      string rebatezhifubaoname = context.Request.Params["RebateZhifubaoName"] == null ? "" : context.Request.Params["RebateZhifubaoName"].ToString().Trim();
      string caifutongaccount = context.Request.Params["CaifutongAccount"] == null ? "" : context.Request.Params["CaifutongAccount"].ToString().Trim();
      string caifutongname = context.Request.Params["CaifutongName"] == null ? "" : context.Request.Params["CaifutongName"].ToString().Trim();
      string shopid = context.Request.Params["ShopID"] == null ? "0" : context.Request.Params["ShopID"].ToString().Trim();
      string cash = context.Request.Params["Cash"] == null ? "" : context.Request.Params["Cash"].ToString().Trim();
      string cellphone = context.Request.Params["Cellphone"] == null ? "" : context.Request.Params["Cellphone"].ToString().Trim();
      string fixphone = context.Request.Params["Fixphone"] == null ? "" : context.Request.Params["Fixphone"].ToString().Trim();
      string receivename = context.Request.Params["ReceiveName"] == null ? "" : context.Request.Params["ReceiveName"].ToString().Trim();
      string receiveaddress = context.Request.Params["ReceiveAddress"] == null ? "" : context.Request.Params["ReceiveAddress"].ToString().Trim();
      string receivecellphone = context.Request.Params["ReceiveCellphone"] == null ? "" : context.Request.Params["ReceiveCellphone"].ToString().Trim();
      string receivefixphone = context.Request.Params["ReceiveFixphone"] == null ? "" : context.Request.Params["ReceiveFixphone"].ToString().Trim();
      string date = context.Request.Params["Date"] == null ? "" : context.Request.Params["Date"].ToString() + " " + DateTime.Now.ToLongTimeString();
      string province = context.Request.Params["hdprovince"] == null ? "" : context.Request.Params["hdprovince"].ToString();
      string city = context.Request.Params["hdcity"] == null ? "" : context.Request.Params["hdcity"].ToString();
      string district = context.Request.Params["hddistrict"] == null ? "" : context.Request.Params["hddistrict"].ToString();
      string beizhu = context.Request.Params["beizhu"] == null ? "" : context.Request.Params["beizhu"].ToString();
      string bankaccount = context.Request.Params["BankAccount"] == null ? "" : context.Request.Params["BankAccount"].ToString().Replace(" ", "");
      string bankname = context.Request.Params["BankName"] == null ? "" : context.Request.Params["BankName"].ToString().Trim();
      string banktype = context.Request.Params["BankType"] == null ? "" : context.Request.Params["BankType"].ToString().Trim();
      string source = context.Request.Params["Source"] == null ? "" : context.Request.Params["Source"].ToString();
      string qqrelation = context.Request.Params["QQRelation"] == null ? "" : context.Request.Params["QQRelation"].ToString().Trim();


      Register r = new Register();
      r.ProId = id;
      r.Qq = qq;
      r.Wangwang = wangwang;
      r.ZhifubaoAccount = zhifubaoaccount;
      r.ZhifubaoName = zhifubaoname;
      r.RebateZhifubao = rebatezhifubao;
      r.RebateZhifubaoName = rebatezhifubaoname;
      r.CaifutongAccount = caifutongaccount;
      r.CaifutongName = caifutongname;
      r.ShopId = Convert.ToInt32(shopid);
      r.Cash = cash;
      r.CellPhone = cellphone;
      r.FixPhone = fixphone;
      r.ReceiveName = receivename;
      r.ReceiveCellphone = receivecellphone;
      r.ReceiveFixphone = receivefixphone;
      r.ReceiveAddress = receiveaddress;
      r.Date = date;
      r.Province = province;
      r.City = city;
      r.District = district;
      r.Beizhu = beizhu;
      r.BankAccount = bankaccount;
      r.BankName = bankname;
      r.BankType = banktype;
      r.Source = source;
      r.QQRelation = qqrelation;

      RegisterBLL.UpdateRegister(r, "ProID", r.ProId.ToString());
      string returnValue = "{QQ:'" + r.Qq + "',Wangwang:'" + r.Wangwang + "',ZhifubaoAccount:'" + r.ZhifubaoAccount + "',ZhifubaoName:'" + r.ZhifubaoName + "',RebateZhifubao:'" + r.RebateZhifubao + "',RebateZhifubaoName:'" + r.RebateZhifubaoName + "',CaifutongAccount:'" + r.CaifutongAccount + "',CaifutongName:'" + r.CaifutongName + "',ShopID:'" + r.ShopId + "',Cash:'" + r.Cash + "',Cellphone:'" + r.CellPhone + "',Fixphone:'" + r.FixPhone + "',ReceiveName:'" + r.ReceiveName + "',ReceiveAddress:'" + r.ReceiveAddress + "',ReceiveCellphone:'" + r.ReceiveCellphone + "',ReceiveFixphone:'" + r.ReceiveFixphone + "',Date:'" + r.Date + "',Source:'" + r.Source + "',QQRelation:'" + r.QQRelation + "'}";

      QQRelationBLL qqrelationBLL = new QQRelationBLL();
      qqrelationModel = new QQRelation();
      if (r.QQRelation != "" && r.QQRelation != null)
      {
        try
        {
          if (oldQQRelation != r.QQRelation)
          {
            q1 = r.Qq;
            q2 = r.QQRelation;

            var dtResult = qqrelationBLL.IsSameQQ(r.Qq, oldQQRelation);
            qqrelationModel.MainQQ = q1;
            qqrelationModel.QQ = q2;
            qqrelationModel.CreateTime = DateTime.Now.ToString();
            if (dtResult.Rows.Count > 0)
            {
              qqrelationModel.QQMappID = Convert.ToInt32(dtResult.Rows[0]["QQMappID"]);
              qqrelationBLL.UpdateQQRelation(qqrelationModel);
              log.Info("更新QQ关联完成。");
            }
            else
            {
              qqrelationBLL.AddQQRelation(qqrelationModel);
              log.Info("插入一条新的QQ关联信息");
            }
          }
        }
        catch (Exception ex)
        {
          log.Error("发现异常信息：" + ex.Message);
        }
      }
      else
      {
        try
        {
          var dtResult = qqrelationBLL.IsSameQQ(r.Qq, oldQQRelation);
          if (dtResult.Rows.Count > 0)
          {
            string qqID = Convert.ToString(dtResult.Rows[0]["QQMappID"]);
            qqrelationBLL.DeleteQQRelation(qqID);
          }
        }
        catch (Exception ex)
        {
          log.Info("删除QQ关联信息的时候出现错误: " + ex.Message);
        }
      }
      context.Response.Write(returnValue);

    }

    private void UpdateQQRelation()
    {

    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}